<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/generic/taglibs.jsp"%>
<s:layout-render name="/WEB-INF/jsp/generic/layout.jsp">
	
	<s:layout-component name="css">
	<link rel="stylesheet" type="text/css" href="${contextPath}/css/admin.css">
	</s:layout-component>

	<s:layout-component name="pageContent">
	<div class="content">

		
		<fieldset>
			<legend>Cliquez ici pour ajouter un membre</legend>
			<s:form action="${actionBean.manageMemberUrl }" focus="first" ><%-- class="collapsed" --%>
				<s:errors globalErrorsOnly="true" />
				<table>
					<tr>
						<td><label for="firstName">Prénom : </label></td>
						<td><s:text name="newMember.firstName" id="firstName" /> <s:errors
								field="newMember.firstName" /></td>
					</tr>
					<tr>
						<td><label for="lastName">Nom : </label></td>
						<td><s:text name="newMember.lastName" id="lastName" /> <s:errors
								field="newMember.lastName" /></td>
					</tr>
					<tr>
						<td><label for="id">Identifiant : </label></td>
						<td><s:text name="newMember.login.id" id="id" /> <s:errors
								field="newMember.login.id" /></td>
					</tr>
					<tr>
						<td><label for="password">Mot de passe : </label></td>
						<td><s:password name="newMember.password" id="password" /> <s:errors
								field="newMember.password" /></td>
					</tr>
					<tr>
						<td><label for="role">Rôle : </label></td>
						<td><s:select name="newMember.roles[0].id">
								<s:options-collection collection="${actionBean.roles.list}"
									value="id" label="label" />
							</s:select> <s:errors field="role" /></td>
					</tr>
					<tr>
						<td><label for="memberDeletable">Supprimable : </label></td>
						<td><s:checkbox name="newMember.deletable" id="memberDeletable" checked="true" /> <s:errors
								field="newMember.deletable" /></td>
					</tr>
					<tr>
						<td />
						<td><s:submit name="createMember">Créer</s:submit>
				</table>
				<%-- <c:if test="${ not empty actionBean.newMember }"><p>Membre créé : ${actionBean.newMember.firstName } ${actionBean.newMember.lastName }</p></c:if> --%>
				<s:messages key="${actionBean.newMemberKey }"/>
			</s:form>
		</fieldset>

		<fieldset>
			<legend>Liste des membres enregistrés</legend>
			<!-- <div class="collapsed"> -->
			<div>
				<c:choose>
					<c:when test="${empty actionBean.members.list}">
						<p>Pas de membres.</p>
					</c:when>
					<c:otherwise>
						<ul>
							<c:forEach items="${actionBean.members.list}" var="member">
								<li>${member.firstName} ${member.lastName} (rôles <c:forEach
										items="${member.roles}" var="role">${role.label}</c:forEach>) <c:if
										test="${member.deletable}">
										<s:form action="${actionBean.manageMemberUrl }">
											<s:errors />
											<input type="hidden" name="idDeletedMember" value="${member.id }"></input>
											<s:submit name="removeMember">supprimer</s:submit>
										</s:form>
									</c:if>
								</li>
	<%--TODO valider--%>
								<li>
									<s:form action="${actionBean.url }" focus="first">
										<s:errors />
										<table>
											<tr>
												<td><label for="firstName">Prénom : </label></td>
												<td><s:text name="firstName" value="${member.firstName}" id="firstName" />
													<s:errors field="${member.firstName }" /></td>
											</tr>
											<tr>
												<td><label for="lastName">Nom : </label></td>
												<td><s:text name="lastName" value="${member.lastName}" id="lastName" />
													<s:errors field="${member.lastName}" /></td>
											</tr>
	
											<tr>
												<td><input type="hidden" name="idModified" value="${member.id }"></input></td>
												<td><s:submit name="modify">modifier</s:submit></td>
											</tr>
										</table>
									</s:form>
								</li>
	<%--TODO fin valider--%>
								</c:forEach>
						</ul>
					</c:otherwise>
				</c:choose>
				
				<%-- pagination --%>
				<p><c:if test="${ actionBean.members.hasPreviousPage }"><a href="${ actionBean.members.previousPageUrl }">Page précédente</a></c:if>
				Page ${ actionBean.members.page } / ${ actionBean.members.pMax }
				<c:if test="${ actionBean.members.hasNextPage }"><a href="${ actionBean.members.nextPageUrl }">Page suivante</a></c:if></p>
				<%-- <p><c:if test="${ actionBean.hasPreviousMemberPage }"><a href="${ actionBean.previousMemberPageUrl }">Page précédente</a></c:if>
				Page ${ actionBean.pm } / ${ actionBean.pmMax }
				<c:if test="${ actionBean.hasNextMemberPage }"><a href="${ actionBean.nextMemberPageUrl }">Page suivante</a></c:if></p> --%>
	
				<%-- <c:if test="${ not empty actionBean.deletedMember }">
					<p>Membre supprimé : ${actionBean.deletedMember.firstName }
						${actionBean.deletedMember.lastName }</p>
				</c:if> --%>
				<s:messages key="${actionBean.deleteMemberKey }"/>
			</div>

		</fieldset>

		<fieldset>
			<legend>Upload d'un fichier CSV de membres</legend>
			<%-- <s:form
				action="<%=BlobstoreServiceFactory.getBlobstoreService()
							.createUploadUrl("/upload")%>"
				method="post" enctype="multipart/form-data"> --%>
			<form
				action="${actionBean.uploadUrl }"
				method="post" enctype="multipart/form-data">
				<s:errors />
				<p>
					<select name="type">
						<option value="excel">Excel</option>
						<option value="csv">CSV</option>
					</select>
					<input type="submit" name="uploadexcel" value="Upload" />
				</p>
			</form>
			<form
				action="${actionBean.uploadUrl }"
				method="post" enctype="multipart/form-data">
				<s:errors />
				<p>
					<input type="file" name="membersCSV" /> <input type="submit" name="uploadcsv"
						value="Upload" />
				</p>
				<fieldset>
					<legend>Paramètres avancés</legend>
					<div class="collapsed">
						<p>
							Séparateur
							<select name="file.separator">
								<c:forEach items="${actionBean.separators }" var="separator">
									<option value='<c:out value="${separator }"></c:out>'><c:out value="${separator }"></c:out></option>
								</c:forEach>
							</select>
							Délimiteur
							<select name="file.quote">
								<c:forEach items="${actionBean.quotechars }" var="quotechar">
									<option value='<c:out value="${quotechar }"></c:out>'><c:out value="${quotechar }"></c:out></option>
								</c:forEach>
							</select>
							Échappement
							<select name="file.escape">
								<c:forEach items="${actionBean.escapes }" var="escape">
									<option value='<c:out value="${escape }"></c:out>'><c:out value="${escape }"></c:out></option>
								</c:forEach>
							</select>
							Encodage
							<select name="file.charset">
								<c:forEach items="${actionBean.charsets }" var="charset">
									<option value="${charset }">${charset }</option>
								</c:forEach>
							</select>
						</p>
						<p>Colonnes du fichier :</p>
						<table>
							
							<tr>
							<c:forEach items="${actionBean.columns }" var="column">
								<td><c:out value="${column.label }"/></td>
							</c:forEach>
							</tr>
							<tr>
							<c:forEach items="${actionBean.columns }" var="column">
								<td><input type="text" name="file.headers['<c:out value="${column.id }"/>']" value="<c:out value="${column.fileColumnName }"/>"/></td>
							</c:forEach>
							</tr>
							
						</table>
					</div>
				</fieldset>
			</form>
		</fieldset>

		<s:form action="${actionBean.url }">
			<s:errors />
			<s:submit name="reindex">Réindexer les membres</s:submit> Vide l'index de recherche et le remplit à nouveau avec les membres enregistrés en base de données.
		</s:form>

		<fieldset>
			<legend>Liste des rôles</legend>
			<c:choose>
				<c:when test="${ empty actionBean.roles.list }">
					<p>Pas de rôles.</p>
				</c:when>
				<c:otherwise>
					<ul>
						<c:forEach items="${actionBean.roles.list}" var="role">
							<li>${role.id} : ${role.label} <c:if
									test="${role.deletable}">
									<s:form action="${actionBean.manageMemberUrl }">
										<%-- <s:errors /> --%>
										<input type="hidden" name="idDeletedRole" value="${role.id }"></input>
										<s:submit name="removeRole">supprimer</s:submit>
									</s:form>
								</c:if>
							</li>
						</c:forEach>
					</ul>
				</c:otherwise>
			</c:choose>
			
			<%-- pagination --%>
			<p><c:if test="${ actionBean.roles.hasPreviousPage }"><a href="${ actionBean.roles.previousPageUrl }">Page précédente</a></c:if>
			Page ${ actionBean.roles.page } / ${ actionBean.roles.pMax }
			<c:if test="${ actionBean.roles.hasNextPage }"><a href="${ actionBean.roles.nextPageUrl }">Page suivante</a></c:if></p>

			<%-- <c:if test="${ not empty actionBean.deletedRole }">
				<p>Rôle supprimé : ${actionBean.deletedRole.label }</p>
			</c:if> --%>
				<s:messages key="${actionBean.deleteRoleKey }"/>


			<s:form action="${actionBean.manageMemberUrl }">
				<%-- <s:errors /> --%>
				<table>
					<tr>
						<td><label for="roleId">Identifiant : </label></td>
						<td><s:text name="newRole.id" id="roleId" /> <s:errors
								field="newRole.id" /></td>
					</tr>
					<tr>
						<td><label for="roleLabel">Libellé : </label></td>
						<td><s:text name="newRole.label" id="roleLabel" /> <s:errors
								field="newRole.label" /></td>
					</tr>
					<tr>
						<td><label for="roleDeletable">Supprimable : </label></td>
						<td><s:checkbox name="newRole.deletable" id="roleDeletable" checked="true" /> <s:errors
								field="newRole.deletable" /></td>
					</tr>
					<tr>
						<td />
						<td><s:submit name="addRole">Créer</s:submit>
				</table>
				<%-- <c:if test="${ not empty actionBean.newRole }"><p>Rôle créé : ${actionBean.newRole.id } ${actionBean.newRole.label }</p></c:if> --%>
				<s:messages key="${actionBean.newRoleKey }"/>
			</s:form>
			
		</fieldset>

		<s:form action="${actionBean.url }">
			<s:errors />
			<s:submit name="resetRoles">Supprimer tous les rôles</s:submit> et rétablir les rôles par défaut. /!\ Attention à supprimer les membres autres qu'administrateurs d'abord !
		</s:form>


	</div>
	
	
	
	
	
	<script type="text/javascript">
	$('fieldset legend').click(function(e) {
		$(this).siblings().slideToggle();
	});
	$('.collapsed').slideToggle(0);
	</script>
	</s:layout-component>

</s:layout-render>